package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.InstaItemBean;

import org.xutils.x;

import java.util.ArrayList;

public class SelectInstallActivity extends BaseActivity implements View.OnClickListener {
    private ArrayList<InstaItemBean.InfoEntity> info1 = new ArrayList<>();
    private int position2;
    private RecyclerView recycler;
    private String rid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_install);
        initTitle();
        info1 = (ArrayList<InstaItemBean.InfoEntity>) getIntent().getSerializableExtra("info");
        rid = getIntent().getStringExtra("rid");
        position2 = getIntent().getIntExtra("position", 0);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recycler.setAdapter(new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_install_select;
            }

            @Override
            public void onBind(final BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.img), info1.get(position).getLogo_pic());
                holder.setTextView(R.id.title, info1.get(position).getGoods_name());
                holder.setTextView(R.id.desc, info1.get(position).getDesc());
                holder.setTextView(R.id.price, info1.get(position).getPrice() + "/" + info1.get(position).getUnit());

                if (info1.get(position).getPid().equals(rid)) {
                    holder.setImageResource(R.id.img2, R.mipmap.ic_choose_li);
                } else {
                    holder.setImageResource(R.id.img2, R.mipmap.ic_choose_nor);
                }
                holder.getView(R.id.select).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = getIntent();
                        intent.putExtra("position", position2);
                        InstaItemBean.InfoEntity infoEntity = info1.get(position);
                        intent.putExtra("bean", infoEntity);
                        setResult(RESULT_OK, intent);
                        finish();

                    }
                });
            }

        });

    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("" + getIntent().getStringExtra("name"));
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        ImageView ic_telephone = (ImageView) findViewById(R.id.ic_telephone);
        ic_telephone.setOnClickListener(this);
        ic_telephone.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.ic_telephone:

                break;
        }
    }
}
